function indexConfig(app,db) {

    //设置页面
    app.post('/setmodelist', async function (req, res, next) {
        let msg = req.body
        let data = {
            msg:'',
            code:200,
            modeList:[],
            mymodeList:[]
        }
        if (req.session.user.id === 0 && msg.password !== req.session.user.psw) {
            data.code = 400
            data.msg = '密码错误'
            res.send(data)
            next()
            return
        }
        if (isNull(msg.modeList) || isNull(msg.mymodeList)) {
            data.code = 300
            res.send(data)
            next()
            return
        }else{
            console.log('首页变更')
            db.serialize(function () {
                db.run('delete from indexmode where userid = '+req.session.user.id,
                function(err){
                    if (err) {
                        console.log(err)
                    }
                })
                db.run('delete from mymode where userid = '+req.session.user.id,
                function(err){
                    if (err) {
                        console.log(err)
                    }
                })
                for (let index = 0; index < msg.modeList.length; index++) {
                    const element = msg.modeList[index]
                    db.run(
                        "insert into indexmode(modename,describe,logo,path,userid)  values(?,?,?,?,?)",
                        element.modename,
                        element.describe,
                        element.logo,
                        element.path,
                        req.session.user.id,
                        function(err){
                            if (err) {
                                console.log(err)
                            }
                        }
                    )
                }
                for (let index = 0; index < msg.mymodeList.length; index++) {
                    const element = msg.mymodeList[index]
                    db.run(
                        "insert into mymode(modename,describe,logo,path,userid)  values(?,?,?,?,?)",
                        element.modename,
                        element.describe,
                        element.logo,
                        element.path,
                        req.session.user.id,
                        function(err){
                            if (err) {
                                console.log(err)
                            }
                        }
                    )
                }
                db.all('SELECT * FROM indexmode where userid = '+req.session.user.id,
                function(err,res){
                    if (!err) {
                        data.modeList = res
                    }
                })
                db.all('SELECT * FROM mymode where userid = '+req.session.user.id,
                function(err,sqres){
                    if (!err) {
                        data.mymodeList = sqres
                    }
                    res.send(data)
                    next()
                })
            })
        }

    })

    //获取页面
    app.post('/getmodelist', async function (req, res, next) {
        db.serialize(function () {
            let data = {
                msg:'',
                code:200,
                userid:req.session.user.id,
                modeList:[],
                mymodeList:[]
            }
            db.all('SELECT * FROM indexmode where userid = '+req.session.user.id,
            function(err,res){
                if (!err) {
                    data.modeList = res
                }
            })
            db.all('SELECT * FROM mymode where userid = '+req.session.user.id,
            function(err,sqres){
                if (!err) {
                    data.mymodeList = sqres
                }
                res.send(data)
                next()
            })
        })
    })
}

function isNull(params) {
    if (params !== undefined && params !== null && params !== '') {
        return false
    }
    return true
}

module.exports ={indexConfig}